/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulation;

import java.awt.Graphics;
import java.util.ArrayList;

/**
 *
 * @author haoliang.donghl
 */
public class Machine {
    public static ArrayList<Machine> allMachines = new ArrayList<Machine>();
    protected String name;
    protected int id;
    private float sum;

    public static Machine getMachineById(int id){
        for (Machine machine : allMachines){
            if (machine.getId() == id) return machine;
        }
        return null;
    }

    public static Machine getMachineByName(String name){
        for (Machine machine : allMachines){
            if (name.equals(machine.getName())) return machine;
        }
        return null;
    }
    
    public static int getMachineNum(){
        return allMachines.size();
    }

    public Machine(){
        allMachines.add(this);
        id = allMachines.size();
        name = "M" + id;
    }

    public int getId(){
        return id;
    }

    public String getName(){
        return name;
    }

    public ArrayList<Instance> getRunningApps(){
        sum = 0;
        ArrayList<Instance> results = new ArrayList<Instance>();
        for (int i = 0; i < Application.getAppNum(); i++){
            Application app = Application.getAppById(i + 1);
            Instance instance = app.getInstance(id);
            if (instance.state == Instance.STATE_RUNNING){
                results.add(instance);
                sum += instance.getLoad();
            }
        }
        return results;
    }

    public void paint(Graphics g){
        ArrayList<Instance> runnings = getRunningApps();
        int offset = 10;
        for (Instance instance : runnings){
            if (id == 1) {
                offset += instance.paint(g, offset, 10, 60);
            }else if (id == 2){
                offset += instance.paint(g, offset, 90, 60);
            }else{
                offset += instance.paint(g, offset, 170, 60);
            }
        }
    }
}
